﻿using Microsoft.OpenApi.Models;

namespace WebServer.Extensions;

public static class SwaggerExtension
{
    public static IServiceCollection AddSwaggerGenService(this IServiceCollection services)
    {
        //添加最小API支持
        services.AddEndpointsApiExplorer();
        //添加Swagger服务
        services.AddSwaggerGen(option =>
        {
            option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
            {
                Description = "请输入正确的Token格式： Bearer xxx",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.ApiKey,
                BearerFormat = "JWT",
                Scheme = "Bearer",                
            });
            // 安全要求
            option.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                     {
                         Reference = new OpenApiReference()
                         {
                             Type = ReferenceType.SecurityScheme,
                             Id = "Bearer"
                         }
                     },
                    new string[]{}                    
                }
            });
        });
        return services;
    }
}
